﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.Entities;
using ECI.BaseLanguage;
using ECI.CommonLibrary;
using ECI.CoreLibrary;

namespace ECI.DynamicData.DataCache
{
    public class EditSQL
    {
        private static ECI.CoreLibrary.CommonLibrary cmn = new ECI.CoreLibrary.CommonLibrary();
        public static string Get(EciTable table, string key)
        {
            if (table.PrimaryKey == null) throw new Exception(Lang.A00055.Read());//没有配置主键
            string sql = "SELECT {0} FROM " + table.Fname + " A  WHERE  " + table.PrimaryKey.Name + "=" + cmn.SQLQ(key);

            string query = "";
            foreach (EciField field in table.ListEdit)
            {
                //2010-06-25 占位符不需要查询
                if (field.DictionaryType == "EMPTY") continue;
                if (field.DictionaryType == "LABEL") continue;
                //如果自定义了如何转换关系则直接用页面配置
                if (field.ShowConvert.Length > 0)
                {

                    string showConvert = field.ShowConvert;
                    showConvert = showConvert.Replace("#USER_ID#", UserInfo.UserId);
                    showConvert = showConvert.Replace("#DEPT_ID#", UserInfo.OperSide);
                    query += "," + showConvert + " AS " + field.Name;
                }
                else
                {
                    if (field.DictionaryType == "CHOOSE" && !field.DropdownlistConfig.Contains("MULTI") && field.Udf5 != "Y")//
                    {
                        string dictionaryTypeConfig = field.DictionaryTypeConfig;
                        if (dictionaryTypeConfig.Trim().Length == 0) throw new Exception(field.MainAlias + Lang.A00056.Read());//字段,没有配置对应的放大镜类型
                        EciDataHelp data = new EciDataHelp();
                        data.StartSetSQLValue();
                        data.QueryType = dictionaryTypeConfig;
                        data.DataType = "CHOOSE";
                        data = data.FetchEntity();
                        if (data.NotFetchData) throw new Exception(Lang.A00057.Read() + "【" + dictionaryTypeConfig + "】");//选择配置中 没有查询到配置
                        string source = data.ConvertSql;

                        string code = data.DataChangeCode;
                        string name = data.DataChangeName;
                        string sourceField = field.DbField;
                        //if (field.DropdownlistConfig.Trim().Length > 0) sourceField = field.DropdownlistConfig;

                        query += string.Format(",( select {0} {5}'|'{5} {1} from ({2}) DUAL where  code=A.{3}) AS  {4}", code, name, source, sourceField, field.Name, DBUtility.Instance.SqlPlusMark());

                    }
                    else
                    {

                        if (string.IsNullOrEmpty(field.JoinTable))
                        {
                            //正常的字段查询
                            //针对数据库类型是日期类型增加 数据格式
                            //2010-09-02 23:17  JACK
                            //2010-04-29 JACK 修改为多数据库支持
                            if (field.PhysicalType == "DATE")
                            {
                                string dataFormat = "";
                                if (field.DataFormatEdit.Trim().Length > 0) dataFormat = field.DataFormatEdit;
                                query += "," + DBUtility.Instance.SqlQToChar(field.DbField, dataFormat) + " AS " + field.Name;
                            }
                            else
                            {
                                query += "," + "A." + field.DbField;
                                if (field.DbField != field.Name) query += " AS " + field.Name;
                            }
                        }
                        else
                        {
                            string myField = "A." + field.Name;
                            if (field.DictionaryType == "CHOOSE")
                            {
                                myField += "{0}'|'{0}B." + field.JoinShowField;
                                myField = string.Format(myField, DBUtility.Instance.SqlPlusMark());
                            }
                            query += ",( SELECT  " + myField + " FROM  " + field.JoinTable + " B WHERE B." + field.JoinTableField + "=A." + field.DbField + ECI.DynamicData.DataCache.QuerySQL.GetJoinCondition(field) + " AND ROWNUM=1) AS " + field.Name;
                        }
                    }
                }
            }

            if (query.Length > 0) query = query.Substring(1);
            if (query == "") query = "1";
            sql = string.Format(sql, query);

            return sql;
        }
    }
}
